function meetingCtrl($scope, $rootScope, $http, $interval, ngDialog, paFetchService) {
  'ngInject';
  let userInfo = JSON.parse(localStorage.getItem('userInfo'));

  // 角色 1代表指挥部，2代表监理单位 3代表施工单位
  $scope.$watch('meeting.selectItem', function(newValue) {
    this.selectItem = newValue;
    if (this.selectItem) {
      this.loginType = this.selectItem.orgType ? this.selectItem.orgType : 1;
    }
    if (this.selectItem) {
      this.loginType = this.selectItem.orgType ? this.selectItem.orgType : 1;
      if (this.selectItem.orgType == 1) {
        this.isShowAddInfoZhb = true;
      } else if ((this.selectItem.orgType == 2 || this.selectItem.orgType == 3) && userInfo.role == 0) {
        this.loginType = 1;
        this.isShowAddInfoZhb = false;
      } else if (this.selectItem.orgType == 2) {
        this.isShowAddInfoZjb = true;
      } else if ((this.selectItem.orgType == 1 || this.selectItem.orgType == 3) && userInfo.role == 1) {
        this.loginType = 2;
        this.isShowAddInfoZjb = false;
      } else if (this.selectItem.orgType == 3) {
        this.isShowAddInfoXmb = true;
      } else if ((this.selectItem.orgType == 1 || this.selectItem.orgType == 2) && userInfo.role == 2) {
        this.loginType = 3;
        this.isShowAddInfoXmb = false;
      }
    }
  }.bind(this));

  // 查看 编辑
  this.check = true;

  this.meetingList = [];
  // 查询安全会议信息
  this.queryData = {};
  this.paginationData = {
    pageNum: 1,
    pageSize: 10,
    total: '',
  };
  // 更换页码时
  $scope.pageChange = () => {
    $http.post(`${SERVER_CONTEXT}/api/securityactivities/securityMeeting/querySecurityMeetingList`,
      angular.extend(this.queryData, this.paginationData)
        )
      .then((resp) => {
        this.meetingList = resp.data.data;
        this.paginationData.total = resp.data.total;
      });
  };
  // 查询
  this.safetyMeeting = () => {
    $http.post(`${SERVER_CONTEXT}/api/securityactivities/securityMeeting/querySecurityMeetingList`,
      angular.extend(this.queryData, this.paginationData)
        ).
      then((resp) => {
        this.meetingList = resp.data.data;
        this.paginationData.total = resp.data.total;
      });
  };

  // 根据条件查询
  this.search = (param) =>{
    if (!param.startTime.$invalid && param.endTime.$invalid) {
      layer.alert('请填写结束时间');
      return false;
    } else if (param.startTime.$invalid && !param.endTime.$invalid) {
      layer.alert('请填写开始时间');
      return false;
    } else if (param.startTime.$valid && param.endTime.$valid) {
      if (param.startTime.$viewValue < param.endTime.$viewValue) {
        this.safetyMeeting();
        return false;
      } else {
        layer.alert('开始时间应小于结束时间');
      }
    } else {
      this.safetyMeeting();
    }
  };

  //重置
  this.reset = () => {
    this.queryData = {
      orgUnit: $rootScope.activeBid.orgId,
    };
  }

  // 新增会议
  this.addMeeting = (ctrl) => {
     ngDialog.open({
      template: 'addMeeting',
      className: 'ngdialog-theme-default pa-special-check-item-list-theme',
      controllerAs: 'meetingAddition',
      scope: $scope,
      controller: function mettingDetailCtrl($http, $scope, paFetchService) {
        'ngInject';

        let _this = this;
        let url;

        this.meetingAdditionList = {
          securityMeetingWorkerList: [],
        };
        // 重置
        this.reset = () => {
          this.meetingAdditionList = {};
        };

        // 上传文件
        // 保存上传文件返回token
        this.profilesToken = [];

        // 参加人员
        //yuxt 人员下拉多选
        this.showTeam = false;
        this.isShowSelect = false;

        // 根据施工队获得人员列表 added by sunqy
        this.showWorkers = (team) => {
          if (team.initFlag) {
            team.isShowWorker = !!!team.isShowWorker;
            return;
          }
          $http
            .get(`${SERVER_CONTEXT}/api/construction/select/${team.teamId}`)
            .then((resp) => {
              team.initFlag = true;
              team.workerInfoBeanList = resp.data;
              team.isShowWorker = !!!team.isShowWorker;
              team.workerInfoBeanList.forEach((worker) => {
                worker.constructionTeamName = team.teamName;
                worker.constructionteamId = team.teamId;
              })
            })
        }

        this.checkAll = function(selectTeam,$event){
          if (!selectTeam.workerInfoBeanList) {
            $http
              .get(`${SERVER_CONTEXT}/api/construction/select/${selectTeam.teamId}`)
              .then((resp) => {
                selectTeam.initFlag = true;
                selectTeam.workerInfoBeanList = resp.data;
                selectTeam.isShowWorker = !!!selectTeam.isShowWorker;
                selectTeam.workerInfoBeanList.forEach((worker) => {
                  worker.constructionTeamName = selectTeam.teamName;
                  worker.constructionteamId = selectTeam.teamId;
                  worker.checked = true;
                })
              })
          }
          var ele = $event.target;
          var flag = $(ele)[0].checked;
          if (flag) {
            selectTeam.isShowWorker = true;
          }
          $.each(this.teamWithOrgList,function(index,value){
            $.each(value.teamList,function(index1,team){
              if(team == selectTeam){
                $.each(team.workerInfoBeanList,function(index2,worker){
                  worker.checked = flag;
                });
              }
            });
          });
        };

        // 判断全选人
        this.isChange = (id) => {
          var bool = true;
          this.teamWithOrgList.forEach((org) => {
            org.teamList.forEach((team) => {
              if (id == team.teamId) {
                team.workerInfoBeanList.forEach((item) => {
                  if(!item.checked){
                      bool = false;
                  }
                })
                team.checked = bool;
              }
            })
          });
        }

        // 保存
        this.save = (param) => {
          // 修改表单是否提交过状态
          param.$submitted = true;

          // 表单校验
          if (param.$invalid) {
            return false;
          }
          // 把文件上传返回token传入this.meetingAdditionList
          this.fileIds = [];
          this.profilesToken.forEach((item) => {
            this.fileIds.push(item.id);
          });
          this.meetingAdditionList.meetingDocuments = this.fileIds.join(',');

          let reqData = this.meetingAdditionList;
          reqData.orgUnitId = $rootScope.activeBid.orgId;
          // 参加人员
          $.each(this.teamWithOrgList, function(index,value){
            $.each(value.teamList, function(index1,team){
              $.each(team.workerInfoBeanList, function(index2,worker){
                if (worker.checked) {
                  reqData.securityMeetingWorkerList.push({
                    workerId: worker.id,
                    constructionteamId: worker.constructionteamId,
                    workerName: worker.workerName
                  });

                  // tempTeams[worker.constructionTeamId] = worker.constructionTeamName;

                }
              });
            });
          });

          // for (var key in tempTeams) {
          //   reqData.educationTrainConTeam.push({
          //     constructionTeamId: key,
          //     constructionTeamName: tempTeams[key],
          //   });
          // }

          if (ctrl.loginType == 1) {
            url = `${SERVER_CONTEXT}/api/securityactivities/securityMeeting/insertSecurityMeetingByZhb`;
          }
          if (ctrl.loginType == 2) {
            url = `${SERVER_CONTEXT}/api/securityactivities/securityMeeting/insertSecurityMeetingByZjb`;
          }
          if (ctrl.loginType == 3) {
            url = `${SERVER_CONTEXT}/api/securityactivities/securityMeeting/insertSecurityMeeting`;
          }
          $http({
            method: 'POST',
            url: url,
            data: reqData
          }).then((resp) => {
            if (resp.data.success) {
              layer.msg(resp.data.message);
              $scope.closeThisDialog();
              $scope.pageChange();
            } else {
              layer.alert(resp.data.message);
            }
          });

        };

        this.initPage = () => {
          // 获得施工队list
          paFetchService
            .getSpecialTeamWithOrgList()
            .then((resp) => {
              this.teamWithOrgList = resp.data;
            });
        }

        this.initPage();
      }
    });
  }

  // 删除会议信息
  this.deleteMeetingItem = (id) => {
    let _this = this;
    $scope.modalTitle = '删除安全会议';
    $scope.modalContent = '确认要删除该会议吗?';
    ngDialog.openConfirm({
      template: 'paConfirm',
      className: 'ngdialog-theme-default pa-confirm-theme',
      scope: $scope,
    }).
    then((value) => {
      let reqData = [], deleteUrl;
      reqData.push(id);
      if (this.loginType == 1) {
        deleteUrl = `${SERVER_CONTEXT}/api/securityactivities/securityMeeting/deleteByPrimaryKeyByZhb/${id}`;
      }
      if (this.loginType == 2) {
        deleteUrl = `${SERVER_CONTEXT}/api/securityactivities/securityMeeting/deleteByPrimaryKeyByZjb/${id}`;
      }
      if (this.loginType == 3) {
        deleteUrl = `${SERVER_CONTEXT}/api/securityactivities/securityMeeting/deleteByPrimaryKey/${id}`;
      }
      $http({
        method: 'DELETE',
        url: deleteUrl
      }).
        then((data) => {
          layer.msg('删除信息成功');
          this.safetyMeeting();
        });

    }, (reason) => {});
  }

  // 查看安全会议
  this.meetingDetailView = (param) => {
    ngDialog.open({
      template: 'meetingDetail',
      className: 'ngdialog-theme-default pa-special-check-item-list-theme',
      controllerAs: 'vm',
      controller: function mettingDetailCtrl($http, $scope) {
        'ngInject';

        this.queryMeeting = () => {
          $http.get(`${SERVER_CONTEXT}/api/securityactivities/securityMeeting/queryById/${param.securityMeetingId}`).
            then((resp) => {
              console.log(resp);
              this.meetingList = resp.data;
              this.fileItemList = resp.data.fileBeanList;

          });
        }

        // 查看更多
        this.getMore = () => {
          this.showContent = !this.showContent;
        }

        // 文件下载
        this.getDownloadSrc = (file) => {
          return `${SERVER_CONTEXT}/api/securityactivities/securityMeeting/download/${file.id}`;
        }

        // 文件预览
        this.preview = (archiveDetail) => {
          let url = $state.href('documentPreview', {
            id: archiveDetail.id,
          });
          window.open(url,'_blank');
        }

        // 初始化页面
        this.initPage = () => {
          // 获取列表详情
          this.queryMeeting();
        }
        this.initPage();
      }
    });
  }

  // 编辑安全会议
  this.meetingDetailDialog = function(param, ctrl) {
    ngDialog.open({
      template: 'meetingDetailList',
      className: 'ngdialog-theme-default pa-special-check-item-list-theme',
      controllerAs: 'meetingDetail',
      scope: $scope,
      controller: function mettingDetailCtrl($http, $scope, $timeout, paFetchService) {
        'ngInject';

        let url;
        let _this = this;
        // 获取列表详情
        this.queryMeeting = () => {
          $http.get(`${SERVER_CONTEXT}/api/securityactivities/securityMeeting/queryById/${param.securityMeetingId}`).
            then((resp) => {
              this.editMeetingList = resp.data;
              this.fileItemList = resp.data.fileBeanList;
              if (!this.editMeetingList.fileBeanList) {
                this.editMeetingList.fileBeanList = [];
              }
              // 获取选中的人员
              this.getCheckedWorker(this.editMeetingList.securityMeetingWorkerList);
          });
        }

        this.getWorkersByTeamId = (team) => {

          if (team.initFlag) {
            team.isShowWorker = !!!team.isShowWorker;
            return;
          }
          $http
            .get(`${SERVER_CONTEXT}/api/securityactivities/securityMeeting/queryMeetingWorkerByCheck/${team.teamId}/${param.securityMeetingId}`)
            .then((resp) => {
              team.initFlag = true;
              team.workerInfoBeanList = resp.data;
              team.isShowWorker = !!!team.isShowWorker;
              team.workerInfoBeanList.forEach((worker) => {
                worker.checked = worker.checked ? true : false;
                worker.constructionTeamName = team.teamName;
                worker.constructionteamId = team.teamId;
              });
              // 判断是否全选
              team.checked = true;
              team.workerInfoBeanList.forEach((worker) => {
                if (!worker.checked){
                  team.checked = false;
                }
              });

            });

        }

        this.getCheckedWorker = (param) => {
          if (param) {
            this.teamWithOrgList.forEach((org) => {
              org.teamList.forEach((team) => {
                param.forEach((item) => {
                  if (item.constructionteamId == team.teamId) {
                    this.getWorkersByTeamId(team);
                  }
                })
              })
            })
          }
        }

        // 根据施工队获得人员列表 added by sunqy
        this.showWorkers = (team) => {
          this.getWorkersByTeamId(team);
        }

        // 全选
        this.checkAll = function(selectTeam,$event){
          if (!selectTeam.workerInfoBeanList) {
            $http
              .get(`${SERVER_CONTEXT}/api/construction/select/${selectTeam.teamId}`)
              .then((resp) => {
                selectTeam.initFlag = true;
                selectTeam.workerInfoBeanList = resp.data;
                selectTeam.isShowWorker = !!!selectTeam.isShowWorker;
                selectTeam.workerInfoBeanList.forEach((worker) => {
                  worker.constructionTeamName = selectTeam.teamName;
                  worker.constructionteamId = selectTeam.teamId;
                  worker.checked = true;
                })
              })
          }

          var ele = $event.target;
          var flag = $(ele)[0].checked;
          if (flag) {
            selectTeam.isShowWorker = true;
          }
          $.each(this.teamWithOrgList,function(index,value){
            $.each(value.teamList,function(inx,team){
              if(team == selectTeam){
                $.each(team.workerInfoBeanList,function(ix,worker){
                  worker.checked = flag;
                });
              }
            });
          });
        };

        // 判断全选人
        this.isChange = (id) => {
          var bool = true;
          this.teamWithOrgList.forEach((org) => {
            org.teamList.forEach((team) => {
              if (id == team.teamId) {
                team.workerInfoBeanList.forEach((item) => {
                  if(!item.checked){
                      bool = false;
                  }
                })
                team.checked = bool;
              }
            })
          })
        }

        // 文件下载
        this.getDownloadSrc = (file) => {
          return `${SERVER_CONTEXT}/api/securityactivities/securityMeeting/download/${file.id}`;
        }


        // 保存修改信息
        this.save = (param) => {
          // 修改表单是否提交过状态
          param.$submitted = true;

          // 表单校验
          if (param.$invalid) {
            return false;
          }

          let fileTokens = this.editMeetingList.fileBeanList;
          this.filesId = [];
          this.editMeetingList.fileBeanList.forEach((item) => {
            this.filesId.push(item.id);
          })
          this.editMeetingList.meetingDocuments = this.filesId.join(',');

          this.editMeetingList.educationTrainConTeam = [];
          this.editMeetingList.securityMeetingWorkerList = [];
          let tempTeams = {};
           $.each(this.teamWithOrgList,function(index,value){
            $.each(value.teamList,function(inx,team){
              $.each(team.workerInfoBeanList,function(ix,worker){
                if (worker.checked) {
                  _this.editMeetingList.securityMeetingWorkerList.push({
                    workerId: worker.workerId ? worker.workerId : worker.id,
                    constructionteamId: worker.constructionteamId,
                    workerName: worker.workerName
                  });

                }
              });
            });
          });

          if (ctrl.loginType == 1) {
            url = `${SERVER_CONTEXT}/api/securityactivities/securityMeeting/updateSecurityMeetingByZhb`;
          }
          if (ctrl.loginType == 2) {
            url = `${SERVER_CONTEXT}/api/securityactivities/securityMeeting/updateSecurityMeetingByZjb`;
          }
          if (ctrl.loginType == 3) {
            url = `${SERVER_CONTEXT}/api/securityactivities/securityMeeting/updateSecurityMeeting`;
          }
          $http.post(url, this.editMeetingList).
            then((resp) => {
              if (resp.data.success) {
                layer.msg(resp.data.message);
                $scope.closeThisDialog();
                $scope.pageChange();
              } else {
                layer.alert(resp.data.message);
              }
            });
        }

        // 初始化页面
        this.initPage = () => {
          // 获得施工队list
          paFetchService
            .getSpecialTeamWithOrgList()
            .then((resp) => {
              this.teamWithOrgList = resp.data;
            });
          // 获取列表详情
          this.queryMeeting();
        }
        this.initPage();
      }
    });
  }

  this.initPage = () => {

    // // 获取安全会议列表
    // this.safetyMeeting();
    // 获得施工队list
    paFetchService
      .getSpecialTeamWithOrgList()
      .then((resp) => {
        this.teamWithOrgList = resp.data;
      });
  }

  // 升序、降序
  this.up = (param,index) => {
    this.queryData.columeName = param;
    this.queryData.order = index;
    this.safetyMeeting();
  }
}

export default meetingCtrl;